package com.feng.mvc.controller;

import com.feng.mvc.bean.User;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class HttpController {
    /**
     * HTTP状态 400 - 错误的请求
     * 类型 状态报告
     *
     * 描述 由于被认为是客户端对错误（例如：畸形的请求语法、无效的请求信息帧或者虚拟的请求路由），服务器无法或不会处理当前请求。
     *
     * Apache Tomcat/9.0.54
     * @return
     */
  /*@RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String requestBody){
        System.out.println("RequestBody: "+ requestBody);
        return "success";
    }*/
    @RequestMapping("/testRequestBody")
    public String testRequestBody(){
//        System.out.println("RequestBody: "+ requestBody);
        return "success";
    }

    @RequestMapping(value = "/testRequestBody",method = RequestMethod.POST)
    public String testRequestBody2(@RequestBody String requestBody){
        System.out.println("RequestBody: "+ requestBody);
        return "success";
    }

    @RequestMapping(value = "/testRequestEntity",method = RequestMethod.POST)
    public String testRequestEntity(RequestEntity<String> requestEntity){
        System.out.println("requestEntity的请求头信息: "+ requestEntity.getHeaders());
        System.out.println("requestEntity的请求体信息: "+ requestEntity.getBody());
        return "success";
    }

    @RequestMapping("/testResponse")
    public void testResponse(HttpServletResponse response) throws IOException {
        response.getWriter().println("Hello response");
    }

    @RequestMapping("/testResponseBody")
    @ResponseBody
    public String testResponseBody(){
        return "Hello ResponseBody";
    }

    /**
     * 添加Jackson 的jar包前：
     *      HTTP状态 500 - 内部服务器错误
     * 加Jackson之后运行正常：
     *     {"id":101,"name":"feng分","email":"feng@163.com","age":17,"gender":"男"}
     *
     * @return
     */
    @RequestMapping("/testResponseBody_user_json")
    @ResponseBody
    public User testResponseBody_user_json(){
        return new User(101,"feng分","feng@163.com",17,"男");
    }

    @RequestMapping("/testAxios")
    @ResponseBody
    public String testAxios(String name,Integer age){
        System.out.println("name: "+name+"\nage: "+age);
        return "Hello Axion";
    }



}
